<?php
include_once '../include/inc_session.php';
?>
<?php
	$oEditions = new dataObject_editions();
	$sNomModule = $oBloc->_MENU_NOM;
	$sEdition =   (isset($_GET['edition'])) ? $_GET['edition'] : "";
	$sDateDebut = (isset($_GET['dd']))      ? $_GET['dd']      : "";
	$sDateFin =   (isset($_GET['df']))      ? $_GET['df']      : "";
	$oElementDico = new dataObject_elementdico();
	$sNomListe;
	$bEnvoiMail = false;
	
	$idUser = $_SESSION['iduser'];
	$oProfil = new dataObject_profil($_SESSION[MM_UserGroup]);
	$oUser = new dataObject_utilisateur($idUser);
	$oFct = new dataObject_fonction($oUser->_IDENTIFIANT_FONCTION);
	
	$aInfoAgent = $oUser->utilisateur_getUniteRegion($oUser->_CODE_UTILISATEUR);
	$bmIsAgent = $oUser->utilisateur_isAgent($idUser);
	
	$bShowTotalGeneral = $bShowTotalRegion = $bShowTotalUnite= false;	
	if($oProfil->_CODE_PROFIL == dataObject_profil::_CODE_AGENT)//agent listes_editions_agent
		$bShowTotalUnite = true;	
	else 
	{
		switch($oFct->_CODE_FONCTION)
		{
			case dataObject_fonction::_FCT_DIR_UNITE:
				//responsable d'unité
				$bShowTotalUnite = true;	
				break;
						
			case dataObject_fonction::_FCT_DIR_REGION:
				//directeur de région
				$bShowTotalUnite = true;	
				$bShowTotalRegion = true;	
				break;
	
			case dataObject_fonction::_FCT_DIR_SIEGE:
				//directeur de siege
				$bShowTotalRegion = true;
				$bShowTotalGeneral = true;	
				break;		
		}
	}
	
	//on envoi le mail si click sur le bouton envoi mail
	if($gbFormValid && isset($_POST['Envoyer']))
	{
		include_once 'succes_envoi_mail.php';
	}
	if(isset($_POST['Retour'])) header('location:index.php');
	
	$aEnteteTab = array(
			array("num_colonne"=>1, "nom_colonne"=>"Contrat"),
			array("num_colonne"=>2, "nom_colonne"=>"Date effet"),
			array("num_colonne"=>3, "nom_colonne"=>"Offre"),
			array("num_colonne"=>4, "nom_colonne"=>"Produit"),
			array("num_colonne"=>5, "nom_colonne"=>"Formule")
		);
		
	switch ($sEdition)
	{
		case '134':
				$sNomListe = $oElementDico->elementdico_getNomFromCode(AE,dataObject_dictionnaire::LISTES_EDITION);
				$aEnteteTab = array_merge($aEnteteTab,array(
				array("num_colonne"=>6, "nom_colonne"=>"Date &eacute;ch&eacute;ance <br>d&eacute;but &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin"),
				array("num_colonne"=>8, "nom_colonne"=>"Montant")
				));
				$sTitre = "Liste des appels d’&eacute;ch&eacute;ance";
				$aPdfWidthRecord_134 = array("Contrat" => 16,
										   "Date effet" => 10,
						"Offre" => 12,
						"Produit" => 12,
						"Formule" => 18,
						"Date &eacute;ch&eacute;ance <br>d&eacute;but &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin" => 18,
						"Montant" => 8,
						"Nombre &eacute;ch&eacute;ances" => 30,
						"Montant &eacute;ch&eacute;ances" => 30
						);
				$sEnteteTab = "
				<tr id=\"niv1\">
					<td class='col2'  style='text-align:center; height:25px; width:40%;'>&nbsp;</td>
					<td class='col2'  style='text-align:center; height:25px; width:".$aPdfWidthRecord_134['Nombre &eacute;ch&eacute;ances']."%; font-weight:bold;'>Nombre &eacute;ch&eacute;ances</td>
					<td class='col2'  style='text-align:center; height:25px; width:".$aPdfWidthRecord_134['Nombre &eacute;ch&eacute;ances']."%; font-weight:bold;'>Montant &eacute;ch&eacute;ances</td>
				</tr>";
				break;
	
		case '136':
				$sNomListe = $oElementDico->elementdico_getNomFromCode(EI,dataObject_dictionnaire::LISTES_EDITION);
				$aEnteteTab = array_merge($aEnteteTab,array(
					array("num_colonne"=>6, "nom_colonne"=>"Date &eacute;ch&eacute;ance <br>d&eacute;but &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin"),
					array("num_colonne"=>7, "nom_colonne"=>"Fr&eacute;quence"),
					array("num_colonne"=>8, "nom_colonne"=>"Montant")
					));
				$sTitre = "Liste des &eacute;ch&eacute;ances impay&eacute;es";
				$aPdfWidthRecord_136 = array("Contrat" => 14,
										   "Date effet" => 10,
						"Offre" => 12,
						"Produit" => 12,
						"Formule" => 18,
						"Date &eacute;ch&eacute;ance <br>d&eacute;but &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin" => 14,
						"Fr&eacute;quence" => 12,
						"Montant" => 8,
						"Nombre impay&eacute;s" => 30,
						"Montant impay&eacute;s" => 30
						);
				$sEnteteTab = "
				<tr id=\"niv1\">
					<td class='col2'  style='text-align:center; height:25px; width:40%;'></td>
					<td class='col2'  style='text-align:center; height:25px; width:".$aPdfWidthRecord_136['Nombre impay&eacute;s']."%; font-weight:bold;'>Nombre impay&eacute;s</td>
					<td class='col2'  style='text-align:center; height:25px; width:".$aPdfWidthRecord_136['Montant impay&eacute;s']."%; font-weight:bold;'>Montant impay&eacute;s</td>
				</tr>";
				break;
	}
$sSepareTable = '</table>
				 <div id="br2"></div>
				 <table class="tab_list" width=100% border="1" style=" border:1px solid black; border-collapse:collapse;">';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<title>Editions</title>
		<link rel="stylesheet" type="text/css" href="../css/main_editions.css" />
            <link rel="stylesheet" type="text/css" href="../css/list_editions.css" />
		<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
        <![endif]-->

<script>   
function printEdition(urlImpression)
    {
    	options = "Width=850,Height=670" ;
    	window.open(urlImpression, "echeances", options) ;
    }
</script>
</head>

<body>
	<div id="sf_menu1">
		<h3><?php echo $sNomModule ?></h3>
		<div id="information_erreur">
		<?php 
			if(isset($sMsgWait)) echo colorer($sMsgWait, "false");
			$aMessages = $oForm->getValidMessage();
			if(!empty($aMessages))
			{
		?>
				<ul id="message">
					<li><?php echo $aMessages[0]['message'] ?></li>
				</ul>
		<?php 
			} 
		?>
		</div>
	</div> <!-- fon sf_menu -->
	<div class="cleared"></div>
	
	<div class="corps1">
			
		<!--/***************************************/-->
			
			<div class="imgleft1"></div>
			
			<div class="imgmiddle1">
			
				<div class="btn">
		        	<span class="btnleft"></span>
		            <span class="btnmiddle">
		            	<h5>Demande d'&eacute;dition</h5>
		            </span><span class="btnright"></span>
		        </div>
				<div class="btn1">
		        	<span class="btnleft"></span>
		            <span class="btnmiddle">
		            	<h5>D&eacute;tail &eacute;dition</h5>
		            </span><span class="btnright"></span>
		        </div>
		        <PDF>
				<table align="center"  >
					<tr id="niv3">
						<td  align="center" ><font size="2"><?php echo $sTitre.' du	'.formaterDate($sDateDebut,"-","/",false).'	au	'.formaterDate($sDateFin,"-","/",false)?></font></td>
					</tr>
				</table>
				<div id="br2"></div>
				<div class="content_table bright">
								
<?php
if (!$bEnvoiMail)
{
	$fTotalGeneral = $iNbtotal = 0;
	$aListesRegions = $oEditions->regionsDirection(dataObject_editions::getIdDirection());
	$idRegionPrec = 0; $sSpaceRegion = '';
	foreach ($aListesRegions as $iReg => $aRegion)
	{
		$iNbreEchRegion = $oEditions->getNbAppelEcheanceRegion($aRegion['Identifiant_region'], $sDateDebut,$sDateFin);
		if($iNbreEchRegion>0)
		{
			$aListesUnites = $oEditions->unitesRegion($aRegion['Identifiant_region']);
		 
			$iNbtotalRegion = $fMntTotalRegion = 0;
			if(($iReg>0) && ($idRegionPrec != 0))
			{
				echo "<p>&nbsp;</p>";
				$sSpaceRegion = "<tr><td>&nbsp;</td></tr>";
			} 
			if(($idRegionPrec != intval($aRegion['Identifiant_region'])) )
			{
	?>
			 <table style="width:400px; text-align: left;" >
			 <?php echo $sSpaceRegion ?>
			    <tr>
				    <td  style="width:20%; text-align: left;">Region : </td>
				    <td  style="width:25%; text-align: left;"><?php echo $aRegion['Code_region']?></td>
				    <td  style="width:55%; text-align: left;"><?php echo $aRegion['Nom_region']?></td>
			    </tr>
			  </table>
	<?php
			}
			$idRegionPrec = intval($aRegion['Identifiant_region']);
			
			foreach ($aListesUnites as $iUnit => $aUnite)
			{
				$sSpaceUnite = '';
				$aListes = array();
				switch ($sEdition)
				{
					case '134':
							$aListes = $oEditions->listeAppelsEcheances($aUnite['Identifiant_unite'],$sDateDebut,$sDateFin);
							break;
	
					case '136':
							$aListes = $oEditions->listeEcheancesImpayees($aUnite['Identifiant_unite'],$sDateDebut,$sDateFin);
							break;
					default : 
							break;
	
				}
				if($iUnit>0) 
				{
						echo "<p>&nbsp;</p>";
						$sSpaceUnite = "<tr><td>&nbsp;</td></tr>";
				}
				if ( !empty($aListes))
				{
					
			?>
		
		    <table style="width:400px; text-align: left;" >
		    	<?php echo $sSpaceUnite ?>
			    <tr>
				    <td  style="width:20%; text-align: left;">Unite  : </td>
				    <td  style="width:25%; text-align: left;"><?php echo $aUnite['Code_unite']?></td>
				    <td  style="width:55%; text-align: left;"><?php echo $aUnite['Nom_unite'] ?></td>
			    </tr>
		    </table>
		    <hr style=" margin-right:17px; width:818px;"/>
		    <table class="tab_list"  style="border:1px solid black; border-collapse:collapse;" border="1">
			<tr id="niv1">
			<?php
				$i = 1;
				foreach ($aEnteteTab as $iKey => $aEntete)
				{
					$sClass = "col".$i;
					if($sEdition == '134')
						if($aEntete['nom_colonne']=='Date &eacute;ch&eacute;ance <br>d&eacute;but &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin') $sClass = "col20";
					if($sEdition == '136')
					{	
						if($aEntete['nom_colonne']=='Contrat') $sClass = "col2";
						if($aEntete['nom_colonne']=='Date effet') $sClass = "col1";
						
						if($aEntete['nom_colonne']=='Date &eacute;ch&eacute;ance <br>d&eacute;but &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin') $sClass = "col13";
					}
					?>
					<td class='<?php echo $sClass ?>'  style='text-align:center; height:25px; width:<?php echo ${'aPdfWidthRecord_'.$sEdition}[$aEntete['nom_colonne']]?>%; font-weight:bold;'><?php echo $aEntete['nom_colonne']?></td>
					<?php
					$i++;
				} 
			?>
			</tr>
	
			<?php
				$fMontantEch = $iNbEch = 0;
				foreach ($aListes as $iEch => $aElement)
				{
					if(($iEch%2)==0) $sColorLine = ' class="colorligne" ';
						else $sColorLine = '';
						
					$iNbEch++;
			?>
		
			<tr id='niv2'  <?php echo $sColorLine;?>>
				<td class='col1' align='center'><?php echo $aElement['Numero_contrat']?></td>
				<td class='col2' align='center'><?php echo formaterDate($aElement['Date_effet_contrat'])?></td>
				<td class='col3'><?php echo htmlentities($aElement['Libelle_offre'] , ENT_COMPAT,'utf-8');?></td>
				<td class='col4'><?php echo htmlentities($aElement['Libelle_produit'] , ENT_COMPAT,'utf-8');?></td>
				<td class='col5'><?php echo htmlentities($aElement['Libelle_formule'] , ENT_COMPAT,'utf-8');?></td>
				<td class='col4' align='center'><?php echo formaterDate($aElement['Date_debut_echeance'])."&nbsp;".formaterDate($aElement['Date_fin_echeance'])?></td>
		
		
			<?php 
				if (isset($aElement['Frequence_paiement']))
				{ 
			?>
						<td class='col8' align='center'><?php echo $oElementDico->elementdico_getNomFromCode($aElement['Frequence_paiement'],'M0007') ?></td>
			<?php 
				} 
			?>
		
			<?php 
						if (isset($aElement['Montant_echeance']))
						{ 
							$fMontantEch += $aElement['Montant_echeance'];
			?>
						<td nowrap="nowrap" align="center"><?php echo $aElement['Montant_echeance']?></td>
			<?php 
						} 
			?>
			</tr>
			<?php 
					}//fin parcours liste echeance
				switch ($sEdition)
				{
					case '134':
						$sTab = $sSepareTable.$sEnteteTab."
						<tr id='niv2'>
							<td class='col2' width='40%'>Unit&eacute; ".$aUnite['Nom_unite']."</td>
							<td class='col2' align='center' width='30%'>".$iNbEch."</td>
							<td class='col2' align='center' width='30%'>".$fMontantEch."</td>
						</tr>";
						break;
						
					case '136':
						$sTab = $sSepareTable.$sEnteteTab."
						<tr id='niv2'>
							<td class='col2' width='40%'>Unit&eacute; ".$aUnite['Nom_unite']."</td>
							<td class='col2' align='center' width='30%'>".$iNbEch."</td>
							<td class='col2' align='center' width='30%'>".$fMontantEch."</td>
						</tr>";
						break;
				}
					
					$iNbtotal += $iNbEch;
					$fTotalGeneral += $fMontantEch;
					$iNbtotalRegion += $iNbEch;
					$fMntTotalRegion += $fMontantEch;
					
					echo $sTab;
				}//fin empty liste
				
			} //fin foreach unité
			
			
			if(($iNbtotalRegion !=0) && ($bShowTotalRegion))
			{
				switch ($sEdition)
				{
					case '134':
						$sTab = $sSepareTable.$sEnteteTab."
						<tr id='niv2'>
							<td class='col2' width='40%'>R&eacute;gion ".$aRegion['Nom_region']."</td>
							<td class='col2' align='center' width='30%'>".$iNbtotalRegion."</td>
							<td class='col2'  align='center' width='30%'>".$fMntTotalRegion."</td>
						</tr>";
						break;
					case '136':
						$sTab = $sSepareTable.$sEnteteTab."
						<tr id='niv2'>
							<td class='col2' width='40%'>R&eacute;gion ".$aRegion['Nom_region']."</td>
							<td class='col2' align='center' width='30%'>".$iNbtotalRegion."</td>
							<td class='col2' align='center' width='30%'>".$fMntTotalRegion."</td>
						</tr>";
						break;
				}		
				echo $sTab;		
			}
		}
	}//fin foreach region
	
	if($bShowTotalGeneral)
	{
		switch ($sEdition)
			{
				case '134':
					$sTab = $sSepareTable.$sEnteteTab."
					<tr id=\"niv2\">
						<td class='col2' width='40%'>Total g&eacute;n&eacute;ral</td>
						<td class='col2' align='center' width='30%'>".$iNbtotal."</td>
						<td class='col2' align='center' width='30%'>".$fTotalGeneral."</td>
					</tr>";
					break;
				case '136':
					$sTab = $sSepareTable.$sEnteteTab."
					<tr id=\"niv2\">
						<td class='col2' width='40%'>Total g&eacute;n&eacute;ral</td>
						<td class='col2' align='center' width='30%'>".$iNbtotal."</td>
						<td class='col2' align='center' width='30%'>".$fTotalGeneral."</td>
					</tr>";
					break;
			}				
	//on affiche les totalisations
	echo $sTab.'</table>';
	}
	
} // fin si: mail non envoyé
?>
</table>
				</div> <!-- fin content table -->
			</PDF>	
			</div>
			<div class="imgright1"></div>	
			<div class="cleared"></div> 
	<?php

	$sHtml = ob_get_contents();
	//$sNomListe = utf8_decode($sNomListe);
	$sNomfichier = preg_replace('/&eacute;/','e',$sNomListe);
	$sNomfichier = preg_replace('/&egrave;/','e',$sNomfichier);
	$sNomfichier = preg_replace('/&#130;/','_',$sNomfichier);
	$sNomfichier = str_replace("d'",'',$sNomfichier);
	$sNomfichier = str_replace("é",'e',$sNomfichier);
	
	if($sNomfichier == 'Liste echeances impayees')
		$sModePrint = 'L';
	else $sModePrint = 'P';

	$oFichierPDF = new CHMTL_PDF($sHtml, $sNomfichier, $sModePrint,false, false);
	$sUrlPrint = "succes_impression.php?file=".$oFichierPDF->sFileEdition;
	?>
	<form action="<?php echo $sEditFormAction ?>" method="post"	name="form1" id="form1" class="formulaire">
		<input type="hidden" name="nomListe" maxlength="50" class="resizeall" value="<?php echo $sNomListe ?>" size="32" />
		<table>
			<tr>
				<td  align="center">ADRESSE MAIL</td>
				<td class="gauche"><input type="text" name="OEM_email" maxlength="50"
					class="resizeall" value="" size="32" /></td>
			</tr>
		</table>
		<input type="hidden" name="file" value="<?php echo $oFichierPDF->sEdition ?>">
		<input type="hidden" name="deb" value="<?php echo formaterDate($sDateDebut,"-","/",false); ?>">
		<input type="hidden" name="fin" value="<?php echo formaterDate($sDateFin,"-","/",false); ?>">
		
		<div class="bt1"><span class="btleft"></span> 
			<a href="../editions"> <input type="submit" name="Retour" value="Retour" class="resizeall btmiddle"/>
			<span class="btright"></span> </a>
		</div>
		
		<div class="bt1">
			<span class="btleft"></span> <input type="submit"
			name="Envoyer" value="Envoyer par mail" class="resizeall btmiddle" />
			<span class="btright"></span>
		</div>
		
	</form>
	
	<div class="bt1">
		<span class="btleft"></span> 
		<input type="submit" name="Imprimer" value="Imprimer" class="resizeall btmiddle" onclick="javascript:printEdition('<?php echo $sUrlPrint ?>');" />
		<span class="btright"></span>
	</div>

	</div>
</body>
</html>
<?php
include_once PATH_INCLUDE.'inc_pied_page.php';

/**
 * fonction qui permet de changer le format d'une date 
 * 
 * @param  string $sDate date à formater : au format "JJ-MM-AAAA", "JJ/MM/AAAA", "AAAA-MM-JJ" ou "AAAA/MM/JJ"
 * @param  char $sSeparateur sépareteur de la date à formater : vaut "-" ou "/"
 * @param  char $NouveauSeparateur sépareteur de la date formater (resultat) : vaut "-" ou "/"
 * @param  bool $bInverser : vaut true si on veut inveser le format de la date etré en parametre et false sinon
 * @return string : au format "JJ-MM-AAAA", "JJ/MM/AAAA", "AAAA-MM-JJ" ou "AAAA/MM/JJ"
 */
function formaterDate($sDate, $sSeparateur = "-", $NouveauSeparateur = "-", $bInverser = true)
{
	$aTab = explode($sSeparateur, $sDate);
	
	if ($bInverser)
		$sRetour = $aTab[2].$NouveauSeparateur.$aTab[1].$NouveauSeparateur.$aTab[0];
	else
		$sRetour = $aTab[0].$NouveauSeparateur.$aTab[1].$NouveauSeparateur.$aTab[2];
	
	return $sRetour;
}
?>